package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbSubject;
import com.ruoyi.basice_info.service.ITbSubjectService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 科目设置Controller
 *
 * @author hongyi
 * @date 2023-12-07
 */
@RestController
@RequestMapping("/basice_info/subject")
public class TbSubjectController extends BaseController
{
    @Autowired
    private ITbSubjectService tbSubjectService;

    /**
     * 查询科目设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSubject tbSubject)
    {
        startPage();
        List<TbSubject> list = tbSubjectService.selectTbSubjectList(tbSubject);
        return getDataTable(list);
    }

    /**
     * 导出科目设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:export')")
    @Log(title = "科目设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSubject tbSubject)
    {
        List<TbSubject> list = tbSubjectService.selectTbSubjectList(tbSubject);
        ExcelUtil<TbSubject> util = new ExcelUtil<TbSubject>(TbSubject.class);
        util.exportExcel(response, list, "科目设置数据");
    }

    /**
     * 获取科目设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:query')")
    @GetMapping(value = "/{subjectId}")
    public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId)
    {
        return success(tbSubjectService.selectTbSubjectBySubjectId(subjectId));
    }

    /**
     * 新增科目设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:add')")
    @Log(title = "科目设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSubject tbSubject)
    {
        addOEdit(tbSubject,"add");
        return toAjax(tbSubjectService.insertTbSubject(tbSubject));
    }

    /**
     * 修改科目设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:edit')")
    @Log(title = "科目设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSubject tbSubject)
    {

        addOEdit(tbSubject,"edit");
        return toAjax(tbSubjectService.updateTbSubject(tbSubject));
    }


    public void addOEdit(TbSubject tbSubject, String type) {
        TbSubject subjectCode = tbSubjectService.getTbSubjectBySubjectCode(tbSubject.getSubjectCode());
        TbSubject subjectName = tbSubjectService.getTbSubjectBySubjectName(tbSubject.getSubjectName());
        if (subjectCode!=null) {
            if (type.equals("add")) {
                throw new IllegalArgumentException("科目代码已经存在");
            } else {
                if (!tbSubject.getSubjectId().equals(subjectCode.getSubjectId())) {
                    throw new IllegalArgumentException("科目代码已经存在");
                }
            }
        }
        if (subjectName!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("科目名称已经存在");
            } else {
                if(!tbSubject.getSubjectId().equals(subjectName.getSubjectId())) {
                    throw new IllegalArgumentException("科目名称已经存在");
                }
            }
        }
    }
    /**
     * 删除科目设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:subject:remove')")
    @Log(title = "科目设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subjectIds}")
    public AjaxResult remove(@PathVariable Long[] subjectIds)
    {
        return toAjax(tbSubjectService.deleteTbSubjectBySubjectIds(subjectIds));
    }
}
